package com.wy.controller;

import com.wy.bean.Base64File;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;

@Controller
@RequestMapping("fileBase64Upload")
@CrossOrigin("*")
public class Base64UploadController {

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestBody Base64File base64FileBean) {
        System.out.println(base64FileBean.getBase64().length());
        // 解码Base64字符串
        byte[] decodedBytes = Base64.getDecoder().decode(base64FileBean.getBase64().split(",")[1]);
        try (OutputStream outputStream = new FileOutputStream("file.jpg")) {
            outputStream.write(decodedBytes);
            System.out.println("文件写入成功！");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件写入失败！");
        }
        return "文件上传成功：" + base64FileBean.getFileName();
    }
}
